package com.kailing.bootBatch.A;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <h3>partitionjob</h3>
 * <p></p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-18 12:48
 **/
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        //1: 使用队列
        //2： 使用2层循环 while
        if (root == null) {
            return res;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            int count = q.size();
            List<Integer> list = new ArrayList<>();
            while (count > 0) {
                //弹出 不删除
                TreeNode treeNode = q.peek();
                //弹出并删除
                q.poll();
                list.add(treeNode.val);
                if(treeNode.left != null) {
                    q.add(treeNode.left);
                }
                if(treeNode.right != null) {
                    q.add(treeNode.right);
                }
                count --;
            }
            res.add(list);
        }
        return res;
    }


    /**
     * 功能描述:
     * <二叉树的层次遍历>
     *
     * @param root
     * @return : void
     * @date : 2019/12/18 12:50
     * @author : hduong
     */
    public List<TreeNode> firstLvlOrder(TreeNode root) {
        List<TreeNode> tempList = new ArrayList<>();
        if (root != null) {
            TreeNode leftNode = root.getLeft();
            if(leftNode != null) {
                tempList.add(leftNode);
            }
            TreeNode rightNode = root.getRight();
            if(rightNode != null) {
                tempList.add(rightNode);
            }
        }
        twoLvlOrder(tempList);
        return tempList;
    }

    private void twoLvlOrder(List<TreeNode> list) {
        for (TreeNode treeNode : list) {

        }
    }

    /**
     * 功能描述: 
     * <遍历节点的左右子孩子>
     * @param node
     * @date : 2019/12/18 13:07
     * @return : void
     * @author : hduong
     */
    private void lvlOrder1(TreeNode node) {
        if(node == null) {
            return;
        }
        if(node.left != null) {
            System.out.print(node.left.val + ",");
        }
        if(node.right != null) {
            System.out.print(node.right.val + ",");
        }
        lvlOrder1(node.left);
        lvlOrder1(node.right);
    }

    /**
     * 1： 使用队列 和 集合看看
     * 2：
     * @param args
     */
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode rootNode = new TreeNode(0);
        TreeNode rootNode1 = new TreeNode(1);
        TreeNode rootNode2 = new TreeNode(2);
        rootNode.setLeft(rootNode1);
        rootNode.setRight(rootNode2);

        TreeNode rootNode3 = new TreeNode(3);
        TreeNode rootNode4 = new TreeNode(4);
        TreeNode rootNode5 = new TreeNode(5);
        TreeNode rootNode6 = new TreeNode(6);
        TreeNode rootNode7 = new TreeNode(7);
        rootNode1.setLeft(rootNode3);
        rootNode1.setRight(rootNode4);

        rootNode2.setLeft(rootNode5);
        rootNode2.setRight(rootNode6);


        rootNode3.setLeft(rootNode7);
        Solution solution1 = new Solution();
        List<List<Integer>> temp = solution1.levelOrder(rootNode);
        for (List<Integer> list : temp) {
            for (Integer integer : list) {
                System.out.print(integer + ",");
            }
            System.out.println();
        }

    }





    /**
     * 树节点 数据结构
     */
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            //默认为null
            left = null;
            right = null;
            val = x;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }



    public List<TreeNode> generateTrees(int n) {
        List<TreeNode> list = new ArrayList<>();

        return list;
    }
}
